﻿@{
    ViewBag.Title = "巡检项目表单";
    Layout = "~/Views/Shared/_FlowForm.cshtml";
}
@System.Web.Optimization.Styles.Render("~/Content/scripts/plugins/jqgrid/css")
@System.Web.Optimization.Scripts.Render("~/Content/scripts/plugins/jqgrid/js")
<script>
    var keyValue = request('keyValue');
    var itemId = request('itemId');
    var parentId = request('parentId');
    var typeId = request('typeId');
    var organizeId = '@LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId';
    var DeviceLevel = [], ProjectType = [], DeviceTypeId = [];
    $(function () {
        initControl();
    })

    //初始化控件
    function initControl() {
        $('#step-1 .panel-body').height($(window).height() - 228);
        $('#Description').height($(window).height() - 380);
        $('#step-2 .tab-content').height($(window).height() - 87);
        //加载导向
        $('#wizard').wizard().on('change', function (e, data) {
            var $finish = $("#btn_finish");
            var $next = $("#btn_next");
            if (data.direction == "next") {
                if (!$('#form1').Validform()) {
                    return false;
                }
                GetTree();
                GetGrid();
                $finish.removeAttr('disabled');
                $next.attr('disabled', 'disabled');
            }
            else {
                $finish.attr('disabled', 'disabled');
                $next.removeAttr('disabled');
            }
        });



        //所属机构
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJson",
            description: "==请选择==",
            height: "250px",
            click: function (item) {
                organizeId = item.id;
            }
        });
        if (!!organizeId) {
            $("#OrganizeId").ComboBoxTreeSetValue(organizeId);
        }

        //设备等级
        $.ajax({
            type: "GET",
            dataType: "json",
            async: false,
            url: "../../ResourceManage/RS_Device/GetDataItemCheckBoxList?EnCode=DeviceLevel",
            success: function (data) {
                debugger
                if (data.count > 0) {
                    $("#DeviceLevel").html(data.html).find("input").change(function () {
                        GetDiviceList();
                    });
                }
                else {
                    $("#DeviceLevel").html("<label style=\"color:red;\">*暂时没有数据，请先设置数据</label>");
                }
            }
        });
        //项目类型
        $.ajax({
            type: "GET",
            dataType: "json",
            async: false,
            url: "../../ResourceManage/RS_Device/GetDataItemCheckBoxList?EnCode=ProjectType",
            success: function (data) {
                debugger
                if (data.count > 0) {
                    $("#ProjectType").html(data.html).find("input").change(function () {
                        GetDiviceList();
                    });
                }
                else {
                    $("#ProjectType").html("<label style=\"color:red;\">*暂时没有数据，请先设置数据</label>");
                }
            }
        });
        //设备种类
        var item = {
            height: $(window).height() - 52,
            url: "../../ResourceManage/RS_DeviceType/GetCheckBoxTreeJson",
            height: "120px",
            showcheck: true,
            cascadecheck: false,
            oncheckboxclick: function (item) {
                debugger;
                var keyValue = item.id;
                if (item.checkstate == 0) {
                    DeviceTypeId.push(keyValue);
                }
                else {
                    DeviceTypeId = $.grep(DeviceTypeId, function (value) {
                        return value != keyValue;
                    });
                }
                GetDiviceList();
            }
        };
        $("#DeviceTypeId").treeview(item);  //初始化

        //巡检专业
        $("#Type").ComboBoxTree({
            url: "../../SystemManage/DataItemDetail/GetDataItemTreeJson",
            param: { EnCode: "PollingType" },
            description: "==请选择==",
            height: "200px",
            click: function (item) {
                typeId = item.id;

                if (item.title == "sbxj") {
                    $("#ProjectTypeTr").removeAttr("hidden");
                    $("#DeviceLevelTr").removeAttr("hidden");
                    $("#DeviceTypeTr").removeAttr("hidden");
                    $("#DeviceTr").removeAttr("hidden");
                    GetGrid2();
                }
                else {
                    $("#ProjectTypeTr").attr("hidden", "hidden");
                    $("#DeviceLevelTr").attr("hidden", "hidden");
                    $("#DeviceTypeTr").attr("hidden", "hidden");
                    $("#DeviceTr").attr("hidden", "hidden");
                    DeviceLevel = [], ProjectType = [], DeviceTypeId = [];
                    $('#DeviceTable').GridUnload();
                }
            }
        });
        if (!!typeId) {
            $("#Type").ComboBoxTreeSetValue(typeId);
        }


        //获取表单
        if (!!keyValue) {
            $.SetForm({
                url: "../../PropertyManage/CS_Polling/GetDetaliFormJson",
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#form1").SetWebControls(data);
                    typeId = data.Type;
                    if ($("#Type").attr("data-text") == "设备巡检") {
                        $("#ProjectTypeTr").removeAttr("hidden");
                        $("#DeviceLevelTr").removeAttr("hidden");
                        $("#DeviceTypeTr").removeAttr("hidden");
                        $("#DeviceTr").removeAttr("hidden");
                        GetGrid2();
                        $.SetForm({
                            url: "../../PropertyManage/CS_Polling/GetRelationDeviceItemList",
                            param: { queryJson: JSON.stringify({ "ItemId": keyValue }) },
                            success: function (data) {
                                if (data.length > 0) {
                                    $(data).each(function (index, item) {
                                        if ($.inArray(item.LevelValue, DeviceLevel) < 0) {
                                            DeviceLevel.push(item.LevelValue);
                                            $("#" + item.LevelValue).attr("checked", true);
                                        }
                                        if ($.inArray(item.ProjectType, ProjectType) < 0) {
                                            ProjectType.push(item.ProjectType);
                                            $("#" + item.ProjectType).attr("checked", true);
                                        }
                                        if ($.inArray(item.TypeId, DeviceTypeId) < 0) {
                                            DeviceTypeId.push(item.TypeId);
                                            $("#DeviceTypeId").setCheckedNodeOne(item.TypeId);
                                        }
                                    });
                                    GetDiviceList();
                                }
                            }
                        })
                    }
                }
            });
        }
    }

    function GetTree() {
        //内容列表树
        var Citem = {
            url: "../../PropertyManage/CS_Polling/GetContentTreeJson",
            param: { Type: typeId, ItemId: keyValue },
            showcheck: true,
            oncheckboxclick: function (item, row) {
                var $gridTable = $("#ContentgridTable");
                if (item.checkstate == 0) {
                    $.ajax({
                        type: "GET",
                        dataType: "json",
                        async: false,
                        url: "../../PropertyManage/CS_Polling/GetContentJson?keyValue=" + item.id,
                        success: function (data) {
                            $gridTable.addRowData(($gridTable.jqGrid("getRowData").length + 1), data);
                        }
                    });
                }
                else {
                    var list = $gridTable.jqGrid("getRowData");
                    $.each(list, function (i, n) {
                        if (n.Id == item.id) {
                            $gridTable.delRowData(i + 1);
                        }
                    });
                }
            }
        };
        $("#itemTree").treeview(Citem);  //初始化
    }
    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $("#ContentgridTable");
        var queryJson = $("#queryCondition").GetWebControls();
        queryJson["condition"] = $('#queryCondition').find('.dropdown-text').attr('data-value');
        queryJson["Type"] = typeId;
        queryJson["ItemId"] = keyValue;
        $gridTable.jqGrid({
            url: "../../PropertyManage/CS_Polling/GetContentList",
            datatype: "json",
            postData: { queryJson: JSON.stringify(queryJson) },
            autowidth: true,
            height: 482,
            colModel: [
                 { label: "主键", name: "Id", index: "Id", hidden: true },
                 { label: "巡检专业", name: "Type", index: "Type", hidden: true },
                 { label: "巡检项目", name: "ItemId", index: "ItemId", hidden: true },
                 { label: "名称", name: "Name", index: "Name", width: 100, align: "left" },
                 { label: "编号", name: "Code", index: "Code", width: 100, align: "left" },
                 { label: "巡检专业", name: "TypeName", index: "TypeName", width: 100, align: "left" },
                  {
                      label: "类型", name: "Category", index: "Category", width: 100, align: "left",
                      formatter: function (cellvalue, options, rowObject) {
                          return cellvalue == 1 ? "选择型" : cellvalue == 0 ? "录入型" : "";
                      }
                  },
                 { label: "标准要求", name: "Criterion", index: "Criterion", width: 150, align: "left" },
                 { label: "检查方式", name: "CheckWay", index: "CheckWay", width: 100, align: "left" },
                 { label: "内容详情", name: "TypeDescription", index: "TypeDescription", hidden: true },
                 {
                     label: "是否必填", name: "IsNeed", index: "IsNeed", width: 80, align: "center",
                     formatter: function (cellvalue, options, rowObject) {
                         return cellvalue == 1 ? "<i value=" + cellvalue + " class=\"fa fa-toggle-on\"></i>" : "<i value=" + cellvalue + " class=\"fa fa-toggle-off\"></i>";
                     }
                 },
                 { label: "备注", name: "Memo", index: "Memo", width: 200, align: "left" }
            ],
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function (rowid, status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selarrrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = $("#queryCondition").GetWebControls();
            queryJson["condition"] = $('#queryCondition').find('.dropdown-text').attr('data-value');
            queryJson["Type"] = typeId;
            queryJson["ItemId"] = keyValue;
            queryJson["keyword"] = $('#txt_Keyword').val();
            $gridTable.jqGrid('setGridParam', {
                url: "../../PropertyManage/CS_Polling/GetContentList",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#txt_Keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }

    function btn_reload() {
        $("#queryCondition .dropdown-text").html("选择条件").removeAttr('data-value');
        $("#txt_Keyword").val("");
        $('#btn_Search').trigger("click");
    }

    //新增
    function btn_add() {
        dialogOpen({
            id: "Form",
            title: '添加内容',
            url: '/PropertyManage/CS_Polling/ContentForm?Flag=1',
            width: "800px",
            height: "520px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick();
            }
        });
    }
    //编辑
    function btn_edit() {
        var keyValue = $("#ContentgridTable").jqGridRowValue("Id");
        var rowid = $("#ContentgridTable").getGridParam('selrow');
        if (!!keyValue || !!rowid) {
            dialogOpen({
                id: "Form",
                title: '编辑内容',
                url: '/PropertyManage/CS_Polling/ContentForm?Flag=1&keyValue=' + keyValue + "&rowid=" + rowid,
                width: "800px",
                height: "520px",
                callBack: function (iframeId) {
                    top.frames[iframeId].AcceptClick();
                }
            });
        }
        else {
            dialogMsg({
                msg: "您没有选中任何数据项,请选中后再操作！",
                type: 0
            })
        }
    }
    //删除
    function btn_delete() {

        var rowid = $("#ContentgridTable").getGridParam('selrow');
        if (rowid) {
            $("#ContentgridTable").delRowData(rowid);
        } else {
            dialogMsg('请选择需要删除的内容！', 0);
        }
    }

    function GetDiviceList() {
        GetJson();
        $('#DeviceTable').GridUnload();
        GetGrid2();
    }
    function GetJson() {
        obj = $("#DeviceLevel input");
        obj1 = $("#ProjectType input");
        DeviceLevel = [];
        ProjectType = [];
        $(obj).each(function (index, item) {
            if (item.checked)
                DeviceLevel.push($(item).attr("id"));
        });
        $(obj1).each(function (index, item) {
            if (item.checked)
                ProjectType.push($(item).attr("id"));
        });
    }
    function GetGrid2() {
        debugger;
        var queryJson = {
            Type: DeviceTypeId,
            Level: DeviceLevel,
            Project: ProjectType,
            OrganizeId: organizeId
        };
        $gridTable = $("#DeviceTable");
        $gridTable.jqGrid({
            url: "../../ResourceManage/RS_Device/GetDeviceList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: 90,
            autowidth: true,
            unwritten: false,
            colModel: [
                { label: "Id", name: "Id", hidden: true },
                { label: "TypeId", name: "TypeId", hidden: true },
                { label: "OrganizeId", name: "OrganizeId", hidden: true },
                { label: "LevelValue", name: "LevelValue", hidden: true },
                { label: "ProjectType", name: "ProjectType", hidden: true },
                { label: "设备名称", name: "Name", index: "Name", width: 100, align: "center", sortable: false },
                { label: "设备编号", name: "Code", index: "Code", width: 100, align: "center", sortable: false },
                { label: "设备类型", name: "Type", index: "Type", align: "center", sortable: false },
                { label: "设备等级", name: "Level", index: "Level", align: "center", sortable: false }
            ],
            viewrecords: true,
            rowNum: 10000,
            sortname: 'Date',
            sortorder: 'desc',
            rownumbers: true,
            gridview: true

        });

    }
    //保存表单
    function AcceptClick() {
        if (!$('#form1').Validform()) {
            return false;
        }
        var postData = $("#form1").GetWebControls(keyValue);
        var Unit = $('#Unit').val();
        postData["TypeName"] = $("#Type").attr('data-text');

        var obj = $("#ContentgridTable").jqGrid("getRowData");
        for (var i = 0; i < obj.length; i++) {
            obj[i].IsNeed = obj[i].IsNeed.indexOf("on") > 0 ? 1 : 0;
            obj[i].Category = obj[i].Category.indexOf("选择型") >= 0 ? 1 : 0;
        }
        var DeviceList = [];
        for (var i = 0; i < DeviceLevel.length; i++) {
            for (var j = 0; j < ProjectType.length; j++) {
                for (var k = 0; k < DeviceTypeId.length; k++) {
                    var device = {};
                    device["Category"] = 1;
                    device["ItemTag"] = 1;
                    device["DeviceLevel"] = DeviceLevel[i];
                    device["ProjectType"] = ProjectType[j];
                    device["DeviceTypeId"] = DeviceTypeId[k];
                    device["OrganizeId"] = organizeId;
                    DeviceList.push(device);
                }
            }
        }
        $.SaveForm({
            url: "../../PropertyManage/CS_Polling/SaveItemDetailForm",
            param: { "keyValue": keyValue, "BaseInfo": JSON.stringify(postData), "ContentList": JSON.stringify(obj), "DeviceList": JSON.stringify(DeviceList) },
            loading: "正在保存数据...",
            success: function () {
                if (top.frames["ItemDetailIndex"]) {
                    top.frames["ItemDetailIndex"].$("#gridTable").trigger("reloadGrid");
                    dialogClose();
                }
                else {
                    $.currentIframe().$("#gridTable").resetSelection();
                    $.currentIframe().$("#gridTable").trigger("reloadGrid");
                }
            }
        })
    }
</script>
<div class="widget-body">
    <div id="wizard" class="wizard" data-target="#wizard-steps">
        <ul class="steps">
            <li data-target="#step-1" class="active"><span class="step">1</span>项目信息配置<span class="chevron"></span></li>
            <li data-target="#step-2"><span class="step">2</span>项目内容配置<span class="chevron"></span></li>
        </ul>
    </div>
    <div class="step-content wizard-step-content" id="wizard-steps">
        <div class="step-pane wizard-step-pane active" id="step-1">
            <div class="panel-body">
                <table class="form">
                    <tr>
                        <td class="formTitle">项目名称<font face="宋体">*</font></td>
                        <td class="formValue">
                            <input id="Name" type="text" class="form-control" placeholder="请输入名称" isvalid="yes" checkexpession="NotNull" />
                        </td>
                    </tr>
                    <tr>
                        <td class="formTitle">项目编号<font face="宋体">*</font></td>
                        <td class="formValue">
                            <input id="Code" type="text" class="form-control" placeholder="请输入编号" isvalid="yes" checkexpession="NotNull" />
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">频次<font face="宋体">*</font></th>
                        <td class="formValue">
                            <div class="checkbox">
                                <label>
                                    <input id="UnitNum" type="text" style="width:40px;" isvalid="yes" checkexpession="NotNull" />
                                    <select id="Unit" style="border: 0px;">
                                        <option value="0">天</option>
                                        <option value="1">周</option>
                                        <option selected="selected" value="2">月</option>
                                        <option value="3">季</option>
                                        <option value="4">年</option>
                                    </select>
                                    <input type="text" style="width:40px;" id="Frenquency" isvalid="yes" checkexpession="NotNull" />次
                                </label>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">巡检专业<font face="宋体">*</font></th>
                        <td class="formValue">
                            <div id="Type" type="selectTree" class="ui-select" isvalid="yes" checkexpession="NotNull"></div>
                        </td>
                    </tr>
                    <tr>
                        <th class="formTitle">所属机构</th>
                        <td class="formValue">
                            <div id="OrganizeId" type="selectTree" class="ui-select"></div>
                        </td>
                    </tr>

                    <tr>
                        <th class="formTitle" valign="top" style="padding-top: 4px;">
                            备注
                        </th>
                        <td class="formValue">
                            <textarea id="Memo" class="form-control" style="height: 70px;"></textarea>
                        </td>
                    </tr>
                    <tr hidden id="DeviceLevelTr">
                        <th class="formTitle">
                            设备等级
                        </th>
                        <td class="formValue">
                            <div id="DeviceLevel" class="checkbox user-select"></div>
                        </td>
                    </tr>
                    <tr hidden id="DeviceTypeTr">
                        <th class="formTitle">
                            设备种类
                        </th>
                        <td class="formValue">
                            <div id="DeviceTypeId" style="border: 1px solid #ccc; box-shadow: inset 0 1px 1px rgba(0,0,0,.075);"></div>
                        </td>
                    </tr>
                    <tr hidden id="ProjectTypeTr">
                        <th class="formTitle">
                            项目类型
                        </th>
                        <td class="formValue">
                            <div id="ProjectType" class="checkbox user-select"></div>
                        </td>
                    </tr>
                    <tr hidden id="DeviceTr">
                        <th class="formTitle">
                            关联设备
                        </th>
                        <td class="formValue">
                            <table id="DeviceTable"></table>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="step-pane flowapp" id="step-2">
            <div style="position:relative;float:left; overflow: auto; height: 100%; width: 20%;">
                <div id="itemTree"></div>
            </div>
            <div style="position: relative; float: left; overflow: auto; height: 100%; width: 1px; border: #ccc 1px solid;"></div>
            <div style="position: relative; float: left; overflow: auto; height: 100%; width: 79.8%; ">
                <div class="titlePanel">
                    <div class="title-search">
                        <table>
                            <tr>
                                <td>
                                    <div id="queryCondition" class="btn-group">
                                        <a class="btn btn-default dropdown-text" data-toggle="dropdown">选择条件</a>
                                        <a class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></a>
                                        <ul class="dropdown-menu">
                                            <li><a data-value="Code">编号</a></li>
                                            <li><a data-value="Name">名称</a></li>
                                            <li hidden><a data-value="TypeName">类别</a></li>
                                        </ul>
                                    </div>
                                </td>
                                <td style="padding-left: 2px;">
                                    <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;" />
                                </td>
                                <td style="padding-left: 5px;">
                                    <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div class="toolbar">
                        <div class="btn-group">
                            <a id="lr-replace" class="btn btn-default" onclick="btn_reload();"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                            <a id="lr-addItem" class="btn btn-default" onclick="btn_add()"><i class="fa fa-plus"></i>&nbsp;新增</a>
                            <a id="lr-editItem" class="btn btn-default" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>
                            <a id="lr-deleteItem" class="btn btn-default" onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                        </div>
                        <script>$('.toolbar').authorizeButton()</script>
                    </div>
                </div>
                <table id="ContentgridTable"></table>
            </div>
        </div>
    </div>
    <div class="form-button" id="wizard-actions">
        <a id="btn_last" disabled class="btn btn-default btn-prev">上一步</a>
        <a id="btn_next" class="btn btn-default btn-next">下一步</a>
        <a id="btn_finish" disabled class="btn btn-success" onclick="AcceptClick();">保存</a>
    </div>
</div>
